package com.tanhua.sso.controller;

import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.pojo.User;
import com.tanhua.dubbo.server.api.ManageFreezeApi;
import com.tanhua.dubbo.server.enums.FreezingRangeEnum;
import com.tanhua.sso.service.SmsService;
import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("user")
public class SmsController {
    //注入smsService对象
    @Autowired
    private SmsService smsService;
    @Reference(version = "1.0.0")
    private ManageFreezeApi manageFreezeApi;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 实现功能：根据手机号获取验证码
     *
     * @param param
     * @return
     */
    @PostMapping("login")
    public ResponseEntity<Object> getCheckCode(@RequestBody Map<String, String> param) {
        ErrorResult errorResult = null;
        try {
            //获取手机号
            String phone = param.get("phone");
            Boolean result = smsService.stateFrozen(phone);
            if (result) {
                //冻结
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.builder().errMessage("账号被冻结，请联系管理员!").errCode("10001").build());
            }
            //不是冻结的
            errorResult = smsService.getCheckCode(phone);
            if (errorResult == null) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);

    }

    /**
     * 实现功能：验证码校验
     */
    @PostMapping("loginVerification")
    public ResponseEntity<Map> loginVerification(@RequestBody Map<String, String> param, HttpServletRequest request) {
        HashMap<String, Object> map = null;
        try {
            String phone = param.get("phone");
            String verificationCode = param.get("verificationCode");
            map = smsService.loginVerification(phone, verificationCode);
            if (map == null) {
                log.error("contro层校验失败!");
                return ResponseEntity.status(500).body(null);
            }
        } catch (Exception e) {
            return ResponseEntity.status(500).body(null);
        }

        /**
         * 保存登录者登录日志
         */
        //获取到登陆者token
        Object token = map.get("token");
        //解析token获取用户user对象
        User user = userInfoService.checkToken(Convert.toStr(token));

        this.smsService.saveLog(request, user.getId());
        return ResponseEntity.ok(map);

    }

}
